package com.qfedu.hotelssm.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.qfedu.hotelssm.dao.DepartmentDao;
import com.qfedu.hotelssm.dao.EmployeeDao;
import com.qfedu.hotelssm.entity.Department;
import com.qfedu.hotelssm.entity.Employee;
import com.qfedu.hotelssm.service.intf.DepartmentService;
import com.qfedu.hotelssm.service.intf.EmployeeService;
import com.qfedu.hotelssm.util.StringUtil;
import com.qfedu.hotelssm.util.ThreadPoolUtil;
import com.qfedu.hotelssm.vo.PageBean;
import com.qfedu.hotelssm.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author truman
 * @date 2022/8/28
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentDao dao;

    @Override
    public R save(Department department) {
        if (dao.save(department) > 0) {
            return R.ok(department.getId());
        } else {
            return R.fail();
        }
    }

    @Override
    public R delete(int id) {
        if (dao.delete(id) > 0) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    @Override
    public R update(Department department) {
        if (dao.update(department) > 0) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    @Override
    public PageBean selete(String name, int pageIndex, int pageSize) {
        int startPage = (pageIndex - 1) * pageSize;
        if (StringUtil.isNotEmpty(name)) {
            name = "%"+name+"%";
        }
        long countEmp = dao.count(name);
        return new PageBean((int) countEmp, dao.pageSelect(name, startPage, pageSize));
    }

    @Override
    public R all() {
        return R.ok(dao.all());
    }

    @Override
    public R selectByid(int id) {
        return R.ok(dao.selectById(id));
    }

    @Override
    public List<Department> download() {
        return dao.all();
    }

    @Override
    public R deletes(int[] ids) {
        if (dao.deletes(ids) > 0) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    @Override
    public R upload(MultipartFile file) throws IOException {
        if (!file.isEmpty()) {
            EasyExcel.read(file.getInputStream(), Department.class, new ReadListener() {
                List<Department> list = new ArrayList<>();
                @Override
                public void invoke(Object o, AnalysisContext analysisContext) {
                    list.add((Department) o);
                    if (list.size() >= 1500) {
                        List<Department> departments = new ArrayList<>(list);
                        list.clear();
                        ThreadPoolUtil.getInstence().executor.execute(() -> {
                            dao.upload(departments);
                        });
                    }
                }
                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                    dao.upload(list);
                }
            }).doReadAll();
        }
        return R.fail();
    }
}
